from django.contrib.auth.models import AbstractUser
from django.db import models
from datetime import datetime
from django.conf import settings


class User(AbstractUser):

    mobile = models.CharField(max_length=11, unique=True, verbose_name="手机号")
    avatar = models.ImageField(null=True, blank=True, verbose_name='用户头像')
    real_name = models.CharField(max_length=32, null=True, verbose_name="真实姓名")
    id_card = models.CharField(max_length=20, null=True, verbose_name="身份证号")

    class Meta:
        db_table = "tb_user"


    def to_dict(self):
        """
        将数据转换后字典
        :return: 字典
        """
        user_dict = {
            "user_id": self.id,
            "name": self.name,
            "mobile": self.mobile,
            "avatar": constants.QINIU_URL_DOMAIN + self.avatar_url if self.avatar_url else "",
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:S")
        }
        return user_dict

    def auto_to_dict(self):
        """
        将实名信息转换成字典
        :return: 字典
        """
        auto_dict = {
            "user_id": self.id,
            "real_name": self.real_name,
            "id_card": self.id_card
        }
        return auto_dict

